Move git macros to using `arg()`
authorAlex Crichton <alex@alexcrichton.com>
Tue, 22 Jul 2014 02:18:23 +0000 (19:18 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Tue, 22 Jul 2014 02:34:17 +0000 (19:34 -0700)
src/cargo/core/source.rs
src/cargo/sources/git/utils.rs
tests/support/mod.rs

index c331c02450131c6859c9d9adca6ba4f31e800cc5..84c7faca283898633e33a54753e1e5a3d7515ee5 100644 (file)
@@ -1,6 +1,7 @@
 use std::fmt;
 use std::fmt::{Show, Formatter};
 use std::hash;
+use std::c_str::CString;
 use serialize::{Decodable, Decoder, Encodable, Encoder};
 
 use url::Url;
@@ -105,6 +106,17 @@ impl Location {
     }
 }
 
+impl<'a> ToCStr for &'a Location {
+    fn to_c_str(&self) -> CString {
+        match **self {
+            Local(ref p) => p.to_c_str(),
+            Remote(ref u) => u.to_string().to_c_str(),
+        }
+    }
+
+    unsafe fn to_c_str_unchecked(&self) -> CString { self.to_c_str() }
+}
+
 impl Show for SourceId {
     fn fmt(&self, f: &mut Formatter) -> fmt::Result {
         match *self {
index 0d8c6e90b330d51740276058984b89e57c15d7b3..1e0e40369a04f6832ccb0d93c85ce24ae2c73b6c 100644 (file)
@@ -4,7 +4,7 @@ use std::io::{UserDir};
 use std::io::fs::{mkdir_recursive,rmdir_recursive};
 use serialize::{Encodable,Encoder};
 
-use core::source::{Location, Local, Remote};
+use core::source::Location;
 use util::{CargoResult, ChainError, ProcessBuilder, process, human};
 
 #[deriving(PartialEq,Clone,Encodable)]
@@ -40,23 +40,15 @@ impl Show for GitReference {
 
 
 macro_rules! git(
-    ($config:expr, $str:expr, $($rest:expr),*) => (
-        try!(git_inherit(&$config, format!($str, $($rest),*)))
-    );
-
-    ($config:expr, $str:expr) => (
-        try!(git_inherit(&$config, format!($str)))
-    );
+    ($config:expr, $($arg:expr),+) => (
+        try!(git_inherit(&$config, process("git")$(.arg($arg))*))
+    )
 )
 
 macro_rules! git_output(
-    ($config:expr, $str:expr, $($rest:expr),*) => ({
-        try!(git_output(&$config, format!($str, $($rest),*)))
-    });
-
-    ($config:expr, $str:expr) => ({
-        try!(git_output(&$config, format!($str)))
-    });
+    ($config:expr, $($arg:expr),*) => ({
+        try!(git_output(&$config, process("git")$(.arg($arg))*))
+    })
 )
 
 macro_rules! errln(
@@ -147,7 +139,7 @@ impl GitRemote {
     }
 
     pub fn has_ref<S: Str>(&self, path: &Path, reference: S) -> CargoResult<()> {
-        git_output!(*path, "rev-parse {}", reference.as_slice());
+        git_output!(*path, "rev-parse", reference.as_slice());
         Ok(())
     }
 
@@ -166,8 +158,8 @@ impl GitRemote {
     }
 
     fn fetch_into(&self, path: &Path) -> CargoResult<()> {
-        Ok(git!(*path, "fetch --force --quiet --tags {} \
-                        refs/heads/*:refs/heads/*", self.fetch_location()))
+        Ok(git!(*path, "fetch", "--force", "--quiet", "--tags",
+                &self.location, "refs/heads/*:refs/heads/*"))
     }
 
     fn clone_into(&self, path: &Path) -> CargoResult<()> {
@@ -175,15 +167,8 @@ impl GitRemote {
 
         try!(mkdir_recursive(path, UserDir));
 
-        Ok(git!(dirname, "clone {} {} --bare --no-hardlinks --quiet",
-                self.fetch_location(), path.display()))
-    }
-
-    fn fetch_location(&self) -> String {
-        match self.location {
-            Local(ref p) => p.display().to_string(),
-            Remote(ref u) => u.to_string(),
-        }
+        Ok(git!(dirname, "clone", &self.location, path, "--bare",
+                "--no-hardlinks", "--quiet"))
     }
 }
 
@@ -204,7 +189,7 @@ impl GitDatabase {
     }
 
     pub fn rev_for<S: Str>(&self, reference: S) -> CargoResult<String> {
-        Ok(git_output!(self.path, "rev-parse {}", reference.as_slice()))
+        Ok(git_output!(self.path, "rev-parse", reference.as_slice()))
     }
 
 }
@@ -247,8 +232,8 @@ impl GitCheckout {
             }));
         }
 
-        git!(dirname, "clone --no-checkout --quiet {} {}",
-             self.get_source().display(), self.location.display());
+        git!(dirname, "clone", "--no-checkout", "--quiet",
+             self.get_source(), &self.location);
 
         Ok(())
     }
@@ -273,38 +258,39 @@ impl GitCheckout {
         // https://www.kernel.org/pub/software/scm/git/docs/RelNotes-1.7.3.txt
         //
         // In this case we just use `origin` here instead of the database path.
-        git!(self.location, "fetch --force --quiet origin");
-        git!(self.location, "fetch --force --quiet --tags origin");
+        git!(self.location, "fetch", "--force", "--quiet", "origin");
+        git!(self.location, "fetch", "--force", "--quiet", "--tags", "origin");
         try!(self.reset(self.revision.as_slice()));
         Ok(())
     }
 
-    fn reset<T: Show>(&self, revision: T) -> CargoResult<()> {
-        Ok(git!(self.location, "reset -q --hard {}", revision))
+    fn reset(&self, revision: &str) -> CargoResult<()> {
+        Ok(git!(self.location, "reset", "-q", "--hard", revision))
     }
 
     fn update_submodules(&self) -> CargoResult<()> {
-        Ok(git!(self.location, "submodule update --init --recursive --quiet"))
+        Ok(git!(self.location, "submodule", "update", "--init",
+                "--recursive", "--quiet"))
     }
 }
 
-fn git(path: &Path, str: &str) -> ProcessBuilder {
-    debug!("Executing git {} @ {}", str, path.display());
+fn git(path: &Path, cmd: ProcessBuilder) -> ProcessBuilder {
+    debug!("Executing {} @ {}", cmd, path.display());
 
-    process("git").args(str.split(' ').collect::<Vec<&str>>().as_slice())
-                  .cwd(path.clone())
+    cmd.cwd(path.clone())
 }
 
-fn git_inherit(path: &Path, str: String) -> CargoResult<()> {
-    git(path, str.as_slice()).exec().chain_error(|| {
-        human(format!("Executing `git {}` failed", str))
+fn git_inherit(path: &Path, cmd: ProcessBuilder) -> CargoResult<()> {
+    let cmd = git(path, cmd);
+    cmd.exec().chain_error(|| {
+        human(format!("Executing {} failed", cmd))
     })
 }
 
-fn git_output(path: &Path, str: String) -> CargoResult<String> {
-    let output = try!(git(path, str.as_slice()).exec_with_output()
-                                                     .chain_error(||
-        human(format!("Executing `git {}` failed", str))));
+fn git_output(path: &Path, cmd: ProcessBuilder) -> CargoResult<String> {
+    let cmd = git(path, cmd);
+    let output = try!(cmd.exec_with_output().chain_error(||
+        human(format!("Executing {} failed", cmd))));
 
     Ok(to_str(output.output.as_slice()).as_slice().trim_right().to_string())
 }
index 4ecbc22a031a7083f099999fb24bffb09c47f1e9..132d223d63a4f969486d46c72b5a0bd4ead85859 100644 (file)
@@ -112,7 +112,6 @@ impl ProjectBuilder {
         process(program)
             .cwd(self.root())
             .env("HOME", Some(paths::home().display().to_string().as_slice()))
-            .extra_path(cargo_dir())
     }
 
     pub fn cargo_process(&self, program: &str) -> ProcessBuilder {